/** Description of SmokeTimeTrackerTask.java 
 *
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.async;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.widget.Button;

public class SmokeTimeTrackerTask extends AsyncTask<Void, Void, Void> {
	
	Context context;
	Boolean isSmoking;
	SharedPreferences prefs;
	Button smokeButton;
	
	public SmokeTimeTrackerTask(Context context, Button smokeButton) {
		this.context = context;
		this.prefs = PreferenceManager.getDefaultSharedPreferences(context);
		this.smokeButton = smokeButton;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if(!prefs.getBoolean("sStatus", false)) {
			isSmoking = false;
		} else {
			isSmoking = true;
			smokeButton.setText("Smoking Now !!!");
			smokeButton.setClickable(false);
		}
		
	}

	@Override
	protected Void doInBackground(Void... params) {
		while(isSmoking) {
			if(!prefs.getBoolean("sStatus", false)) {
				isSmoking = false;
			} else {
				isSmoking = true;
			}
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		if(!isSmoking) {
			smokeButton.setText("Smoke Now !!!");
			smokeButton.setClickable(true);
		}
	}	
	
}
